package com.haste.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.haste.common.domain.po.BaseElementPo;
import com.haste.common.domain.po.BaseNoteContentPo;
import com.haste.common.domain.po.BaseNotePo;
import com.haste.common.service.IBaseElementService;
import com.haste.common.service.IBaseNoteContentService;
import com.haste.common.service.IBaseNoteService;
import com.haste.system.domain.vo.NoteVo;
import com.haste.system.service.INoteService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author haste
 */
@Service
public class NoteServiceImpl implements INoteService {

    @Resource
    private IBaseNoteService baseNoteService;

    @Resource
    private IBaseNoteContentService baseNoteContentService;

    @Resource
    private IBaseElementService baseElementService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(NoteVo noteVo) {
        String userId = "2";
        noteVo.setUserId(userId);
        baseNoteService.save(noteVo);

        List<BaseNoteContentPo> noteContentList = noteVo.getNoteContentList();
        for (BaseNoteContentPo baseNoteContentPo : noteContentList) {
            baseNoteContentPo.setNoteId(noteVo.getId());
        }
        baseNoteContentService.saveBatch(noteVo.getNoteContentList());
    }

    @Override
    public void update(NoteVo noteVo) {
        baseNoteService.updateById(noteVo);
        baseNoteContentService.updateBatchById(noteVo.getNoteContentList());
    }

    /**
     * 1、查询note
     * 2、查询内容
     */
    @Override
    public List<NoteVo> list() {
        String userId = "2";
        LambdaQueryWrapper<BaseNotePo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseNotePo::getUserId,userId);
        queryWrapper.eq(BaseNotePo::getDisabled,2);
        List<BaseNotePo> list = baseNoteService.list(queryWrapper);
        if (CollUtil.isEmpty(list)){
            return null;
        }
        List<NoteVo> res = new ArrayList<>();

        LambdaQueryWrapper<BaseNoteContentPo> contentQueryWrapper;
        LambdaQueryWrapper<BaseElementPo> elementQueryWrapper;
        for (BaseNotePo baseNotePo : list) {
            NoteVo noteVo = new NoteVo();
            BeanUtil.copyProperties(baseNotePo,noteVo);
            contentQueryWrapper = new LambdaQueryWrapper<>();
            contentQueryWrapper.eq(BaseNoteContentPo::getNoteId,baseNotePo.getId());
            List<BaseNoteContentPo> contentList = baseNoteContentService.list(contentQueryWrapper);
            noteVo.setNoteContentList(contentList);

            elementQueryWrapper = new LambdaQueryWrapper<>();
            elementQueryWrapper.eq(BaseElementPo::getElementId,baseNotePo.getId());
            elementQueryWrapper.eq(BaseElementPo::getUserId,baseNotePo.getUserId());
            BaseElementPo element = baseElementService.getOne(elementQueryWrapper);
            if (null != element){
                noteVo.setPositionY(element.getPositionY());
                noteVo.setPositionX(element.getPositionX());
            }
            res.add(noteVo);
        }
        return res;
    }
}
